import 'package:flutter/material.dart';
import 'package:flutter_chat/common/style/styles.dart';
import 'package:gap/gap.dart';
import 'package:get/get.dart';

/// 询问框
class DialogUtil {
  /// 确认按钮字体颜色
  static const Color cancelFontColor = Colors.black;

  /// 取消按钮颜色
  static const Color cancelColor = Color(0xfff0f0f0);

  /// 显示警告提示框
  static void showWarningDialog({
    String title = "提示",
    required String content,
    Widget? contentWidget,
    String confirmTitle = "确定",
    String cancelTitle = "取消",
    Function()? onConfirm,
    Function()? onCancel,
  }) {
    customDialog(
      titleBottomGap: AppTheme.largeSpace,
      titleWidget: const Icon(
        Icons.error,
        color: AppTheme.warningColor,
        size: 50,
      ),
      bodyWidget: contentWidget ??
          Text(
            content,
            style: const TextStyle(
              color: Colors.black,
              fontWeight: FontWeight.bold,
            ),
          ),
      buttonWidget: buildButtonGroup(
        cancelTitle: cancelTitle,
        onCancel: onCancel ??
            () {
              Get.back();
            },
        confirmTitle: confirmTitle,
        onConfirm: onConfirm ??
            () {
              Get.back();
            },
      ),
    );
  }

  /// 显示错误提示框
  static void showErrorDialog({
    String title = "提示",
    required String content,
    Widget? contentWidget,
    String buttonTitle = "确定",
    Function()? onTap,
  }) {
    customDialog(
      titleBottomGap: AppTheme.largeSpace,
      titleWidget: const Icon(
        Icons.highlight_off,
        color: AppTheme.dangerColor,
        size: 50,
      ),
      bodyWidget: contentWidget ??
          Text(
            content,
            style: const TextStyle(
              color: Colors.black,
              fontWeight: FontWeight.bold,
            ),
          ),
      buttonWidget: Row(
        children: [
          Expanded(
            child: buildButton(
              title: buttonTitle,
              onPressed: onTap ??
                  () {
                    Get.back();
                  },
            ),
          )
        ],
      ),
    );
  }

  /// 显示成功提示框
  static void showSuccessDialog({
    String title = "提示",
    required String content,
    Widget? contentWidget,
    String buttonTitle = "确定",
    Function()? onTap,
  }) {
    customDialog(
      titleBottomGap: AppTheme.largeSpace,
      titleWidget: const Icon(
        Icons.check_circle,
        color: AppTheme.successColor,
        size: 50,
      ),
      bodyWidget: contentWidget ??
          Text(
            content,
            style: const TextStyle(
              color: Colors.black,
              fontWeight: FontWeight.bold,
            ),
          ),
      buttonWidget: Row(
        children: [
          Expanded(
            child: buildButton(
              title: buttonTitle,
              onPressed: onTap ??
                  () {
                    Get.back();
                  },
            ),
          )
        ],
      ),
    );
  }

  /// 显示提示框
  static void showSingleButtonDialog({
    String title = "提示",
    required String content,
    Widget? contentWidget,
    String buttonTitle = "确定",
    Function()? onTap,
  }) {
    customDialog(
      title: title,
      bodyWidget: contentWidget ??
          Text(
            content,
            style: const TextStyle(
              color: Colors.black54,
              fontWeight: FontWeight.bold,
            ),
          ),
      buttonWidget: Row(
        children: [
          Expanded(
            child: buildButton(
              title: buttonTitle,
              onPressed: onTap ??
                  () {
                    Get.back();
                  },
            ),
          )
        ],
      ),
    );
  }

  /// 显示询问框
  static void showConfirmDialog({
    String title = "提示",
    required String content,
    Widget? contentWidget,
    String confirmTitle = "确定",
    String cancelTitle = "取消",
    Function()? onConfirm,
    Function()? onCancel,
  }) {
    customDialog(
      title: title,
      bodyWidget: contentWidget ??
          Text(
            content,
            style: const TextStyle(
              color: Colors.black54,
              fontWeight: FontWeight.bold,
            ),
          ),
      buttonWidget: buildButtonGroup(
        cancelTitle: cancelTitle,
        confirmTitle: confirmTitle,
        onConfirm: onConfirm,
        onCancel: onCancel,
      ),
    );
  }

  /// 构建按钮组
  static Widget buildButtonGroup({
    String confirmTitle = "确定",
    String cancelTitle = "取消",
    Function()? onConfirm,
    Function()? onCancel,
  }) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        Expanded(
          child: buildButton(
            title: cancelTitle,
            onPressed: onCancel ??
                () {
                  Get.back();
                },
            color: cancelColor,
            fontColor: cancelFontColor,
          ),
        ),
        const SizedBox(width: AppTheme.largeSpace),
        Expanded(
          child: buildButton(
            title: confirmTitle,
            onPressed: onConfirm ??
                () {
                  Get.back();
                },
          ),
        ),
      ],
    );
  }

  /// 构建按钮
  static Widget buildButton({
    required String title,
    required Function() onPressed,
    Color? color,
    Color? fontColor,
  }) {
    return SizedBox(
      height: 35,
      child: ElevatedButton(
        style: ButtonStyle(
          backgroundColor: MaterialStateProperty.all(
            color ?? AppTheme.primaryColor,
          ),
          shape: MaterialStateProperty.all<RoundedRectangleBorder>(
            RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(15),
            ),
          ),
        ),
        onPressed: onPressed,
        child: Text(
          title,
          style: TextStyle(
            color: fontColor,
          ),
        ),
      ),
    );
  }

  /// 自定义询问框
  static Future<T?> customDialog<T>({
    String title = "询问框",
    Widget? titleWidget,
    required Widget bodyWidget,
    Widget? buttonWidget,
    double titleBottomGap = 20,
  }) {
    BuildContext context = Get.context!;
    return Navigator.of(context).push<T>(DialogRoute(
      context: context,
      barrierDismissible: false,
      builder: (context) {
        return Stack(
          alignment: Alignment.center,
          children: [
            Container(
              padding: const EdgeInsets.symmetric(
                vertical: AppTheme.largeSpace,
                horizontal: AppTheme.largeSpace,
              ),
              decoration: BoxDecoration(
                shape: BoxShape.rectangle,
                color: Colors.white,
                borderRadius: BorderRadius.circular(15),
                boxShadow: const [
                  BoxShadow(
                    color: Colors.black12,
                    offset: Offset(0, 10),
                    blurRadius: AppTheme.radius,
                  ),
                ],
              ),
              width: Get.width - (Get.width * 0.2),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  titleWidget ??
                      Text(
                        title,
                        style: const TextStyle(
                          fontSize: AppTheme.largeFont,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                  Gap(titleBottomGap),
                  bodyWidget,
                  Visibility(
                    visible: buttonWidget != null,
                    child: const Gap(20),
                  ),
                  if (buttonWidget != null) buttonWidget,
                ],
              ),
            )
          ],
        );
      },
    ));
  }
}
